<?php

namespace App\Http\Controllers;

use App\Project;
use App\Services\AwardService;
use App\Services\LogService;
use App\Services\CommonService;
use Auth, Request;
use App\Services\AuthService;
use Illuminate\Support\Facades\DB;
use Session;

class AdminWebController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', [
            'except' => [
                'index',
                'login',
            ],
        ]);
        $this->authSvc = new AuthService;
        $this->awardSvc = new AwardService;
        $this->logSvc = new LogService;
        $this->commonSvc = new CommonService;

        $this->middleware('check_role');
    }

    public function index()
    {
        $user = Request::user();
        if ($user) {
            if ($user->isAdmin()) {
                return redirect('/admin/list-projects?aside_key=形式审查-进步奖&award_id=1&status_ids=' . Project::STATUS_FORMAL_REVIEW_TO_REVIEW);
            } elseif ($user->isStaff()) {
                return view('admin.index',['user' => $user]);
            } else {
                return view('404',['tip' => '权限不足，无法访问']);
            }
        } else {
            $loginRole = Session::get('login_role');
            if ($loginRole == null) {
                $redirectTo = '/admin/login';
            } else {
                $redirectTo = '/admin/login';
            }
            return redirect($redirectTo);
        }
    }

    public function login()
    {
        $ip = Request::getClientIp();
//        $errorSessionKey = 'count_of_login_error_' . $ip;
        $countOfLoginError = 0;
//        if (Session::has($errorSessionKey)) {
//            $countOfLoginError = Session::get($errorSessionKey);
//        }
        return view('admin.login', ['countOfLoginError' => $countOfLoginError]);
    }

    public function listProjects()
    {
        $user = Request::user();
        return view('admin.list-projects', ['user' => $user]);
    }

    public function assignReview()
    {
        $user = Request::user();
        return view('admin.assign-review', ['user' => $user]);
    }

    public function expertReview()
    {
        $user = Request::user();
        return view('admin.expert-review', ['user' => $user]);
    }

    public function meetingReview()
    {
        $user = Request::user();
        $largeGroupId = Request::input('large_group_id');
        $awardId = DB::table('large_groups')->where('id', $largeGroupId)->value('award_id');
        return view('admin.meeting-review', ['user' => $user, 'awardId' => $awardId]);
    }

    public function planAward() {
        $user = Request::user();
        return view('admin.plan-award', ['user' => $user]);
    }

    public function prepareDefense() {
        $user = Request::user();
        return view('admin.prepare-defense', ['user' => $user]);
    }

    public function award() {
        $user = Request::user();
        return view('admin.award', ['user' => $user]);
    }

    public function manageProjectGroups()
    {
        $user = Request::user();
        return view('admin.manage-project-groups', ['user' => $user]);
    }

    public function manageReviewerGroups()
    {
        $user = Request::user();
        return view('admin.manage-reviewer-groups', ['user' => $user]);
    }

    public function manageCommissioners()
    {
        $user = Request::user();
        if (!$this->authSvc->canUserMangeAllUser($user)) {
            die('非管理员无权查看');
        }
        return view('admin.manage-commissioners', ['user' => $user]);
    }

    public function manageReviewers()
    {
        $user = Request::user();
        if (!$this->authSvc->canUserMangeAllUser($user)) {
            die('非管理员无权查看');
        }
        return view('admin.manage-reviewers', ['user' => $user]);
    }

    public function manageApplicants()
    {
        $user = Request::user();
        if (!$this->authSvc->canUserMangeAllUser($user)) {
            die('非管理员无权查看');
        }
        return view('admin.manage-applicants', ['user' => $user]);
    }
    public function manageFeedbacks()
    {
        $user = Request::user();
        if (!$user->isAdmin()) {
            die('非管理员无权查看');
        }
        return view('admin.manage-feedbacks', ['user' => $user]);
    }

    public function certifyOrgs()
    {
        $user = Request::user();
        $orgUsers = $this->authSvc->getOrgCertifyUser();
        return view('admin.certify-orgs', ['user' => $user, 'orgUsers' => $orgUsers]);
    }

    public function managePower() {
        $user = Request::user();
        return view('admin.manage-power', ['user' => $user]);
    }

    public function manageLoginError(){
        $user = Request::user();
        return view('admin.manage-login-error', ['user' => $user]);
    }

}
